// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.question.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.TestQuestion;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.modules.question.controller.request.DealTestQuestionReq;
import com.liuxinlong.modules.question.controller.request.QueryTestQuestionReq;
import com.liuxinlong.modules.question.controller.request.UpdateTestQuestionReq;
import com.liuxinlong.modules.question.service.TestQuestionService;
import com.liuxinlong.utils.ThreadlocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试问题管理控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022年4月02日
 */
@RestController
@RequestMapping("/question/test")
public class TestQuestionController {
    @Autowired
    private TestQuestionService testQuestionService;

    /**
     * 分页查询测试问题信息
     *
     * @param req 请求信息
     * @return 测试问题信息列表
     */
    @PostMapping("page")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.QUESTION_TEST,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageTestQuestionInfo(@RequestBody QueryTestQuestionReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("title", req.getTitle());
        queryMap.put("menu", req.getMenu());
        queryMap.put("status", req.getStatus());
        queryMap.put("levelList", StringUtils.isNotEmpty(req.getSeverityLevel()) ?  Arrays.asList(req.getSeverityLevel().split(",")) : null);
        queryMap.put("createUser", req.getCreateUser());
        queryMap.put("startTime", req.getStartTime());
        queryMap.put("endTime", req.getEndTime());
        User currentUser = (User)ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        if (req.isCurrent()) {
            queryMap.put("operateUser", currentUser.getId());
        }
        List<Map<String, Object>> dataList = testQuestionService.pageTestQuestionInfo(queryMap);
        int totalNum = testQuestionService.getTestQuestionCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询测试问题信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增测试问题信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value ="新增测试问题信息",business = LogBusinessConstants.QUESTION_TEST,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addTestQuestionInfo(@RequestBody UpdateTestQuestionReq req) {
        TestQuestion testQuestion = new TestQuestion();
        testQuestion.setTitle(req.getTitle());
        testQuestion.setMenu(req.getMenu());
        testQuestion.setContent(req.getContent());
        testQuestion.setSeverityLevel(req.getSeverityLevel());
        testQuestion.setAttach(req.getAttach());
        testQuestion.setRemark(req.getRemark());
        testQuestion.setOperateUser(req.getOperateUser());
        testQuestionService.addTestQuestionInfo(testQuestion);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增测试问题信息成功");
        return resp;

    }

    /**
     * 修改测试问题信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value ="更新测试问题信息",business = LogBusinessConstants.QUESTION_TEST,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateTestQuestionInfo(@RequestBody UpdateTestQuestionReq req) {
        TestQuestion testQuestion = new TestQuestion();
        testQuestion.setId(req.getId());
        testQuestion.setTitle(req.getTitle());
        testQuestion.setMenu(req.getMenu());
        testQuestion.setContent(req.getContent());
        testQuestion.setSeverityLevel(req.getSeverityLevel());
        testQuestion.setAttach(req.getAttach());
        testQuestion.setRemark(req.getRemark());
        testQuestion.setOperateUser(req.getOperateUser());
        testQuestionService.updateTestQuestionInfo(testQuestion);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改测试问题信息成功");
        return resp;
    }

    /**
     * 删除测试问题信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value ="删除测试问题信息",business = LogBusinessConstants.QUESTION_TEST,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteTestQuestionInfo(@RequestBody BaseDeleteReq req) {
        testQuestionService.deleteTestQuestionInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除测试问题信息成功");
        return resp;
    }

    /**
     * 导出测试问题信息
     *
     * @param req 请求信息
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value ="导出测试问题信息",business = LogBusinessConstants.QUESTION_TEST,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportTestQuestionInfo(@RequestBody BaseDeleteReq req, HttpServletResponse response) {
        testQuestionService.exportTestQuestionInfo(req.getId(),response);
    }

    /**
     * 处理测试问题
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("deal")
    @LogAnnotation(value ="处理测试问题信息",business = LogBusinessConstants.QUESTION_TEST,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp dealTestQuestionInfo(@RequestBody DealTestQuestionReq req) {
        testQuestionService.dealTestQuestionInfo(req.getId(),req.getAssign(),req.getDealResult(),req.getDealOpinions());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("处理测试问题成功");
        return resp;
    }

    /**
     * 查询测试问题处理记录
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("record")
    @LogAnnotation(value ="查询测试问题处理记录",business = LogBusinessConstants.QUESTION_TEST,operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryDealRecordList(@RequestBody BaseDeleteReq req) {
        List<Map<String,Object>> dataList = testQuestionService.queryDealRecordList(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setRetMessage("查询测试问题处理记录成功");
        return resp;
    }
}
